package com.atdfrz.secondshop.controller.user;

import com.atdfrz.secondshop.common.Constants;
import com.atdfrz.secondshop.mapper.CategoryMapper;
import com.atdfrz.secondshop.mapper.ProductMapper;
import com.atdfrz.secondshop.pojo.Category;
import com.atdfrz.secondshop.pojo.Product;

import com.atdfrz.secondshop.service.CarouselService;
import com.atdfrz.secondshop.vo.IndexCarouselVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

//import static com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean.LOGGER;

@Controller
@RequestMapping("/front")
public class ProductController extends BaseController{


    @Resource
    private CarouselService newBeeMallCarouselService;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @RequestMapping("/index")
    public String getFL(Model model, HttpServletRequest request) {
        //查询所有分类显示于首页
        List<IndexCarouselVO> carousels = newBeeMallCarouselService.getCarouselsForIndex( Constants.INDEX_CAROUSEL_NUMBER);
        request.setAttribute("carousels", carousels);//轮播图
        List<Category> category = categoryMapper.selectList(null);
        model.addAttribute("category",category);
        return "front/index";
    }

    @GetMapping("/getproduct")
    public String getdetail(Model model,Integer productId){
        //根据商品ID查找该商品的详情
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(productId!=null,Product::getProductId,productId);
        Product productdetail = productMapper.selectOne(wrapper);
        model.addAttribute("productdetail",productdetail);
        //查询所有分类显示于详情页
        List<Category> category = categoryMapper.selectList(null);
        model.addAttribute("category",category);
        return "front/proDetail";
    }

    @GetMapping("/getcategory")
    public String getcategory(Model model,Integer categoryId,String sort){
        //根据商品分类ID查找该分类商品
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        if(sort.equals("新品上市")){
            wrapper.eq(Product::getState,1).eq(categoryId!=null,Product::getCategoryId,categoryId).orderByDesc(Product::getCreateTime);
        }else if(sort.equals("销量从高到低")){
            wrapper.eq(Product::getState,1).eq(categoryId!=null,Product::getCategoryId,categoryId).orderByDesc(Product::getSales);
        }else if(sort.equals("销量从低到高")){
            wrapper.eq(Product::getState,1).eq(categoryId!=null,Product::getCategoryId,categoryId).orderByAsc(Product::getSales);
        }else if(sort.equals("价格从高到低")){
            wrapper.eq(Product::getState,1).eq(categoryId!=null,Product::getCategoryId,categoryId).orderByDesc(Product::getPrice);
        }else if (sort.equals("价格从低到高")){
            wrapper.eq(Product::getState,1).eq(categoryId!=null,Product::getCategoryId,categoryId).orderByAsc(Product::getPrice);
        }else {
            wrapper.eq(Product::getState,1).eq(categoryId!=null,Product::getCategoryId,categoryId);
        }
        List<Product> products = productMapper.selectList(wrapper);
        model.addAttribute("products",products);
        //根据获取的分类ID显示该商品页属于哪一个商品类
        LambdaQueryWrapper<Category> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(categoryId!=null,Category::getCategoryId,categoryId);
        List<Category> categories = categoryMapper.selectList(wrapper1);
        model.addAttribute("categories",categories);
        //查询所有分类显示于商品页
        List<Category> category = categoryMapper.selectList(null);
        model.addAttribute("category",category);
        return "front/products";
    }

    @RequestMapping("/release")
    public String getSPFL(Model model, HttpSession session) {
        //查询所有分类显示于用户添加页
        List<Category> category = categoryMapper.selectList(null);
        model.addAttribute("category",category);
        //根据用户id搜索其发布的商品
        Integer userid = getUidFromSession(session);
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(userid!=null,Product::getUserId,userid);
        List<Product> products = productMapper.selectList(wrapper);
        model.addAttribute("products",products);
        return "front/release";
    }
    @RequestMapping("/search")
    public String getserch(Model model, @RequestParam("search") String search) {
        //查询所有分类显示于查询页
        List<Category> category = categoryMapper.selectList(null);
        model.addAttribute("category",category);
        //模糊查询商品
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(search!=null,Product::getName,search);
        List<Product> products = productMapper.selectList(wrapper);
        model.addAttribute("products",products);
        //用于展示查询字符
        model.addAttribute("search",search);
        return "front/search";
    }

    @RequestMapping("/topmargin")
    public String toPublicview(Model model) {
        //查询所有分类显示于用户添加页
        List<Category> category = categoryMapper.selectList(null);
        model.addAttribute("category",category);
        return "front/public/topmargin";
    }




}
